package com.tools.common.object.type.handler;

import com.fasterxml.jackson.core.JsonParser;
import com.tools.common.container.*;
import com.tools.common.container.r.*;
import com.tools.common.exception.ParseRuntimeException;
import com.tools.common.object.C;
import com.tools.common.object.Note;
import com.tools.common.object.ObjKit;
import com.tools.common.object.json.DeserializerHandler;
import com.tools.common.object.type.CTKit;

import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * Set 集合的类型处理器
 * */
@Note("Set 集合的类型处理器")
public final class SetTypeHandler {

    private SetTypeHandler() {}

    /* ************************************************************************************************************
     *
     *          转换的功能方法
     *
     * ************************************************************************************************************
     * */

    @Note("将 source 转换为 Set 集合")
    public static Set castToSet(Object source, C<?> targetType) {
        Class<?> mainType = targetType.getMainType();
        if(HashSet.class.equals(mainType) || Set.class.equals(mainType)) {
            return hashSet(source, targetType);
        }
        if(LinkedHashSet.class.equals(mainType)) {
            return linkedHashSet(source, targetType);
        }
        if(ConcurrentHashSet.class.equals(mainType)) {
            return concurrentHashSet(source, targetType);
        }
        if(TreeSet.class.equals(mainType)) {
            return treeSet(source, targetType);
        }
        if(ConcurrentSkipListSet.class.equals(mainType)) {
            return concurrentSkipListSet(source, targetType);
        }
        if(OnlyReadHashSet.class.equals(mainType)) {
            return onlyReadHashSet(source, targetType);
        }
        if(OnlyReadLinkedSet.class.equals(mainType)) {
            return onlyReadLinkedSet(source, targetType);
        }
        if(OnlyReadTreeSet.class.equals(mainType)) {
            return onlyReadTreeSet(source, targetType);
        }
        if(CopyOnWriteArraySet.class.equals(mainType)) {
            return copyOnWriteArraySet(source, targetType);
        }
        if(AbstractList.class.equals(mainType) || AbstractCollection.class.equals(mainType)) {
            return hashSet(source, targetType);
        }
        return otherSet(source, targetType);
    }


    @Note("使用 JsonParser 流模式将 JSON 转为 Set 集合")
    public static Set castToSet(JsonParser parser, C<?> targetType) {
        Class<?> mainType = targetType.getMainType();
        try {
            if(HashSet.class.equals(mainType) || Set.class.equals(mainType)) {
                HashSet set = hashSet(targetType);
                return (Set) CTKit.addJsonDataInCollection(parser, set, targetType);
            }
            if(LinkedHashSet.class.equals(mainType)) {
                LinkedHashSet set = linkedHashSet(targetType);
                return (Set) CTKit.addJsonDataInCollection(parser, set, targetType);
            }
            if(ConcurrentHashSet.class.equals(mainType)) {
                ConcurrentHashSet set = concurrentHashSet(targetType);
                return (Set) CTKit.addJsonDataInCollection(parser, set, targetType);
            }
            if(TreeSet.class.equals(mainType)) {
                TreeSet set = treeSet(targetType);
                try {
                    return (Set) CTKit.addJsonDataInCollection(parser, set, targetType);
                } catch (ClassCastException e) {
                    TreeSet res = new TreeSet(DeserializerHandler.getDefaultComparator());
                    res.addAll(set);
                    return (Set) CTKit.addJsonDataInCollection(parser, res, targetType);
                }
            }
            if(ConcurrentSkipListSet.class.equals(mainType)) {
                ConcurrentSkipListSet set = concurrentSkipListSet(targetType);
                try {
                    return (Set) CTKit.addJsonDataInCollection(parser, set, targetType);
                } catch (ClassCastException e) {
                    ConcurrentSkipListSet res = new ConcurrentSkipListSet(DeserializerHandler.getDefaultComparator());
                    res.addAll(set);
                    return (Set) CTKit.addJsonDataInCollection(parser, res, targetType);
                }
            }
            if(OnlyReadHashSet.class.equals(mainType)) {
                HashSet set = onlyReadHashTempSet(targetType);
                CTKit.addJsonDataInCollection(parser, set, targetType);
                return new OnlyReadHashSet(set);
            }
            if(OnlyReadLinkedSet.class.equals(mainType)) {
                LinkedHashSet set = onlyReadLinkedTempSet(targetType);
                CTKit.addJsonDataInCollection(parser, set, targetType);
                return new OnlyReadLinkedSet(set);
            }
            if(OnlyReadTreeSet.class.equals(mainType)) {
                HashSet temp = onlyReadHashTempSet(targetType);
                Set set = (Set) CTKit.addJsonDataInCollection(parser, temp, targetType);
                Object second = targetType.getArgValue(1);
                if(second instanceof Comparator) {
                    return new OnlyReadTreeSet((Comparator) second, set);
                }
                return new OnlyReadTreeSet(DeserializerHandler.getDefaultComparator(), set);
            }
            if(CopyOnWriteArraySet.class.equals(mainType)) {
                HashSet temp = copyOnWriteArraySetTempHashSet(targetType);
                Set set = (Set) CTKit.addJsonDataInCollection(parser, temp, targetType);
                return new CopyOnWriteArraySet(set);
            }
            if(AbstractSet.class.equals(mainType)) {
                HashSet set = hashSet(targetType);
                return (Set) CTKit.addJsonDataInCollection(parser, set, targetType);
            }
            Set set = (Set) ObjKit.buildObject(targetType);
            return (Set) CTKit.addJsonDataInCollection(parser, set, targetType);
        } catch (Exception e) {
            throw new ParseRuntimeException(e);
        }
    }


    @Note("将 source 转换为 AbsOwnSet 集合")
    public static AbsOwnSet castToAbsOwnSet(Object source, C<?> targetType) {
        Class<?> mainType = targetType.getMainType();
        if(OwnSet.class.equals(mainType) || AbsOwnSet.class.equals(mainType)) {
            return ownSet(source, targetType);
        }
        return otherOwnSet(source, targetType);
    }


    @Note("使用 JsonParser 流模式将 JSON 转为 AbsOwnList 集合")
    public static AbsOwnSet castToAbsOwnSet(JsonParser parser, C<?> targetType) {
        Class<?> mainType = targetType.getMainType();
        try {
            if(OwnSet.class.equals(mainType) || AbsOwnSet.class.equals(mainType)) {
                OwnSet set = ownSet(targetType);
                return (AbsOwnSet) CTKit.addJsonDataInOwnRows(parser, set, targetType);
            }
            AbsOwnSet set = (AbsOwnSet) ObjKit.buildObject(targetType);
            return (AbsOwnSet) CTKit.addJsonDataInOwnRows(parser, set, targetType);
        } catch (Exception e) {
            throw new ParseRuntimeException(e);
        }
    }

    /* ************************************************************************************************************
     *
     *          构建空集合的方法
     *
     * ************************************************************************************************************
     * */


    @Note("新建空的 HashSet")
    public static HashSet hashSet(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        Object second = targetType.getArgValue(1);
        if(first instanceof Integer) {
            int f = MapEnum.calcInitSize((Integer) first);
            if(second instanceof Float || second instanceof Double) {
                float s = ((Number) second).floatValue();
                return new HashSet(f, s);
            }
            return new HashSet(f);
        }
        if(first instanceof Collection) {
            return new HashSet((Collection) first);
        }
        return new HashSet<>();
    }


    @Note("新建空的 LinkedHashSet")
    public static LinkedHashSet linkedHashSet(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        Object second = targetType.getArgValue(1);
        if(first instanceof Integer) {
            int f = MapEnum.calcInitSize((Integer) first);
            if(second instanceof Float || second instanceof Double) {
                float s = ((Number) second).floatValue();
                return new LinkedHashSet(f, s);
            }
            return new LinkedHashSet(f);
        }
        if(first instanceof Collection) {
            return new LinkedHashSet((Collection) first);
        }
        return new LinkedHashSet();
    }

    @Note("新建空的 ConcurrentHashSet")
    public static ConcurrentHashSet concurrentHashSet(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        Object second = targetType.getArgValue(1);
        Object third = targetType.getArgValue(2);
        if(first instanceof Integer) {
            int f = MapEnum.calcInitSize((Integer) first);
            if(second instanceof Float || second instanceof Double) {
                float s = ((Number) second).floatValue();
                if(third instanceof Integer) {
                    int t = (Integer) third;
                    return new ConcurrentHashSet(f, s, t);
                }
                return new ConcurrentHashSet(f, s);
            }
            return new ConcurrentHashSet(f);
        }
        if(first instanceof Iterable) {
            return new ConcurrentHashSet((Iterable) first);
        }
        if(first != null && first.getClass().isArray()) {
            return new ConcurrentHashSet((Object[]) first);
        }
        return new ConcurrentHashSet();
    }




    @Note("新建空的 TreeSet")
    public static TreeSet treeSet(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        if(first instanceof Comparator) {
            return new TreeSet((Comparator) first);
        }
        if(first instanceof SortedSet) {
            SortedSet f = (SortedSet) first;
            try {
                return new TreeSet(f);
            } catch (ClassCastException e) {
                TreeSet treeSet = new TreeSet(DeserializerHandler.getDefaultComparator());
                treeSet.addAll(f);
                return treeSet;
            }
        }
        if(first instanceof Collection) {
            Collection f = (Collection) first;
            try {
                return new TreeSet(f);
            } catch (ClassCastException e) {
                TreeSet treeSet = new TreeSet(DeserializerHandler.getDefaultComparator());
                treeSet.addAll(f);
                return treeSet;
            }
        }
        return new TreeSet();
    }


    @Note("新建空的 ConcurrentSkipListSet")
    public static ConcurrentSkipListSet concurrentSkipListSet(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        if(first instanceof Comparator) {
            return new ConcurrentSkipListSet((Comparator) first);
        }
        if(first instanceof SortedSet) {
            SortedSet f = (SortedSet) first;
            try {
                return new ConcurrentSkipListSet(f);
            } catch (ClassCastException e) {
                ConcurrentSkipListSet set = new ConcurrentSkipListSet(DeserializerHandler.getDefaultComparator());
                set.addAll(f);
                return set;
            }
        }
        if(first instanceof Collection) {
            Collection f = (Collection) first;
            try {
                return new ConcurrentSkipListSet(f);
            } catch (ClassCastException e) {
                ConcurrentSkipListSet set = new ConcurrentSkipListSet(DeserializerHandler.getDefaultComparator());
                set.addAll(f);
                return set;
            }
        }
        return new ConcurrentSkipListSet();
    }


    @Note("获取空的临时的 HashSet，用于后续生成只读 HashSet -> OnlyReadHashSet")
    public static HashSet onlyReadHashTempSet(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        if(first instanceof Iterable) {
            HashSet set = new HashSet();
            CTKit.iterableToCollection((Iterable) first, set);
            return set;
        }
        if(first != null && first.getClass().isArray()) {
            HashSet set = new HashSet();
            Collections.addAll(set, (Object[]) first);
            return set;
        }
        return new HashSet();
    }


    @Note("获取空的临时的 LinkedHashSet，用于后续生成只读 LinkedHashSet -> OnlyReadLinkedSet")
    public static LinkedHashSet onlyReadLinkedTempSet(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        if(first instanceof Iterable) {
            LinkedHashSet set = new LinkedHashSet();
            CTKit.iterableToCollection((Iterable) first, set);
            return set;
        }
        if(first != null && first.getClass().isArray()) {
            LinkedHashSet set = new LinkedHashSet();
            Collections.addAll(set, (Object[]) first);
            return set;
        }
        return new LinkedHashSet();
    }

    @Note("构建空的临时的 HashSet，用于后续生成 CopyOnWriteArraySet 集合")
    public static HashSet copyOnWriteArraySetTempHashSet(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        if(first instanceof Collection) {
            return new HashSet((Collection) first);
        }
        return private_newHashSetByOrArray(first);
    }


    @Note("构建空的 OwnSet 集合")
    public static OwnSet ownSet(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        if(first == null) return new OwnSet();
        if(first instanceof Integer) {
            return new OwnSet((Integer) first);
        }
        if(first.getClass().isArray()) {
            return new OwnSet((Object[]) first);
        }
        if(first instanceof Iterable) {
            Iterable f = (Iterable) first;
            Object second = targetType.getArgValue(1);
            if(second instanceof Comparator) {
                return new OwnSet(f, (Comparator) second);
            }
            return new OwnSet(f);
        }
        if(first instanceof Comparator) {
            Comparator f = (Comparator) first;
            Object second = targetType.getArgValue(1);
            if(second != null && second.getClass().isArray()) {
                return new OwnSet(f, (Object[]) second);
            }
            return new OwnSet(f);
        }
        if(first instanceof SetEnum) {
            SetEnum f = (SetEnum) first;
            Object second = targetType.getArgValue(1);
            if(second == null) return new OwnSet(f);
            if(second instanceof Integer) {
                return new OwnSet(f, (Integer) second);
            }
            if(second.getClass().isArray()) {
                return new OwnSet(f, (Object[]) second);
            }
            if(second instanceof Comparator) {
                Object third = targetType.getArgValue(2);
                return new OwnSet(f, (Comparator) second, (Object[]) third);
            }
            if(second instanceof Iterable) {
                Iterable s = (Iterable) second;
                Object third = targetType.getArgValue(2);
                if(third instanceof Comparator) {
                    return new OwnSet(f, s, (Comparator) third);
                }
                return new OwnSet(f, s);
            }
        }
        return new OwnSet();
    }

    /* ************************************************************************************************************
     *
     *          构建完整数据集合的功能方法
     *
     * ************************************************************************************************************
     * */

    @Note("HashSet 集合的类型转换")
    public static HashSet hashSet(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Iterable.class.isAssignableFrom(sourceClass)) {
            HashSet set = hashSet(targetType);
            CTKit.iterableToCollectionFillIn((Iterable) source, set, targetType);
            return set;
        }
        if(sourceClass.isArray()) {
            HashSet set = hashSet(targetType);
            CTKit.arrayToCollectionFillIn((Object[]) source, set, targetType);
            return set;
        }
        throw new ClassCastException("无法将 " + sourceClass.getName() +
                " 的 【" + source + "】 转为 java.util.HashSet");
    }


    @Note("LinkedHashSet 集合的类型转换")
    public static LinkedHashSet linkedHashSet(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Iterable.class.isAssignableFrom(sourceClass)) {
            LinkedHashSet set = linkedHashSet(targetType);
            CTKit.iterableToCollectionFillIn((Iterable) source, set, targetType);
            return set;
        }
        if(sourceClass.isArray()) {
            LinkedHashSet set = linkedHashSet(targetType);
            CTKit.arrayToCollectionFillIn((Object[]) source, set, targetType);
            return set;
        }
        throw new ClassCastException("无法将 " + sourceClass.getName() +
                " 的 【" + source + "】 转为 java.util.LinkedHashSet");
    }



    @Note("ConcurrentHashSet 集合的类型转换")
    public static ConcurrentHashSet concurrentHashSet(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Iterable.class.isAssignableFrom(sourceClass)) {
            ConcurrentHashSet set = concurrentHashSet(targetType);
            CTKit.iterableToCollectionFillIn((Iterable) source, set, targetType);
            return set;
        }
        if(sourceClass.isArray()) {
            ConcurrentHashSet set = concurrentHashSet(targetType);
            CTKit.arrayToCollectionFillIn((Object[]) source, set, targetType);
            return set;
        }
        throw new ClassCastException("无法将 " + sourceClass.getName() +
                " 的 【" + source + "】 转为 com.tools.common.container.ConcurrentHashSet");
    }


    @Note("TreeSet 集合的类型转换")
    public static TreeSet treeSet(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Iterable.class.isAssignableFrom(sourceClass)) {
            TreeSet set = treeSet(targetType);
            Iterable src = (Iterable) source;
            try {
                CTKit.iterableToCollectionFillIn(src, set, targetType);
                return set;
            } catch (ClassCastException e) {
                TreeSet res = new TreeSet(DeserializerHandler.getDefaultComparator());
                res.addAll(set);
                CTKit.iterableToCollectionFillIn(src, res, targetType);
                return res;
            }
        }
        if(sourceClass.isArray()) {
            TreeSet set = treeSet(targetType);
            Object[] src = (Object[]) source;
            try {
                CTKit.arrayToCollectionFillIn(src, set, targetType);
                return set;
            } catch (ClassCastException e) {
                TreeSet res = new TreeSet(DeserializerHandler.getDefaultComparator());
                res.addAll(set);
                CTKit.arrayToCollectionFillIn(src, res, targetType);
                return res;
            }
        }
        throw new ClassCastException("无法将 " + sourceClass.getName() +
                " 的 【" + source + "】 转为 java.util.TreeSet");
    }


    @Note("ConcurrentSkipListSet 集合的类型转换")
    public static ConcurrentSkipListSet concurrentSkipListSet(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Iterable.class.isAssignableFrom(sourceClass)) {
            ConcurrentSkipListSet set = concurrentSkipListSet(targetType);
            Iterable src = (Iterable) source;
            try {
                CTKit.iterableToCollectionFillIn(src, set, targetType);
                return set;
            } catch (ClassCastException e) {
                ConcurrentSkipListSet res = new ConcurrentSkipListSet(DeserializerHandler.getDefaultComparator());
                res.addAll(set);
                CTKit.iterableToCollectionFillIn(src, res, targetType);
                return res;
            }
        }
        if(sourceClass.isArray()) {
            ConcurrentSkipListSet set = concurrentSkipListSet(targetType);
            Object[] src = (Object[]) source;
            try {
                CTKit.arrayToCollectionFillIn(src, set, targetType);
                return set;
            } catch (ClassCastException e) {
                ConcurrentSkipListSet res = new ConcurrentSkipListSet(DeserializerHandler.getDefaultComparator());
                res.addAll(set);
                CTKit.arrayToCollectionFillIn(src, res, targetType);
                return res;
            }
        }
        throw new ClassCastException("无法将 " + sourceClass.getName() +
                " 的 【" + source + "】 转为 java.util.concurrent.ConcurrentSkipListSet");
    }


    @Note("只读 HashSet 集合的类型转换")
    public static OnlyReadHashSet onlyReadHashSet(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Iterable.class.isAssignableFrom(sourceClass)) {
            HashSet temp = onlyReadHashTempSet(targetType);
            CTKit.iterableToCollectionFillIn((Iterable) source, temp, targetType);
            return new OnlyReadHashSet(temp);
        }
        if(sourceClass.isArray()) {
            HashSet temp = onlyReadHashTempSet(targetType);
            CTKit.arrayToCollectionFillIn((Object[]) source, temp, targetType);
            return new OnlyReadHashSet(temp);
        }
        throw new ClassCastException("无法将 " + sourceClass.getName() +
                " 的 【" + source + "】 转为 com.tools.common.container.r.OnlyReadHashSet");
    }

    @Note("只读 LinkedHashSet 集合的类型转换")
    public static OnlyReadLinkedSet onlyReadLinkedSet(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Iterable.class.isAssignableFrom(sourceClass)) {
            LinkedHashSet temp = onlyReadLinkedTempSet(targetType);
            CTKit.iterableToCollectionFillIn((Iterable) source, temp, targetType);
            return new OnlyReadLinkedSet(temp);
        }
        if(sourceClass.isArray()) {
            LinkedHashSet temp = onlyReadLinkedTempSet(targetType);
            CTKit.arrayToCollectionFillIn((Object[]) source, temp, targetType);
            return new OnlyReadLinkedSet(temp);
        }
        throw new ClassCastException("无法将 " + sourceClass.getName() +
                " 的 【" + source + "】 转为 com.tools.common.container.r.OnlyReadLinkedSet");
    }


    @Note("生成 OnlyReadTreeSet 只读集合")
    public static OnlyReadTreeSet onlyReadTreeSet(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Iterable.class.isAssignableFrom(sourceClass)) {
            HashSet temp = onlyReadHashTempSet(targetType);
            CTKit.iterableToCollectionFillIn((Iterable) source, temp, targetType);
            Object second = targetType.getArgValue(1);
            if(second instanceof Comparator) {
                return new OnlyReadTreeSet((Comparator) second, temp);
            }
            return new OnlyReadTreeSet(temp, DeserializerHandler.getDefaultComparator());
        }
        if(sourceClass.isArray()) {
            HashSet temp = onlyReadHashTempSet(targetType);
            CTKit.arrayToCollectionFillIn((Object[]) source, temp, targetType);
            Object second = targetType.getArgValue(1);
            if(second instanceof Comparator) {
                return new OnlyReadTreeSet((Comparator) second, temp);
            }
            return new OnlyReadTreeSet(temp, DeserializerHandler.getDefaultComparator());
        }
        throw new ClassCastException("无法将 " + sourceClass.getName() +
                " 的 【" + source + "】 转为 com.tools.common.container.r.OnlyReadTreeSet");
    }


    @Note("CopyOnWriteArraySet 集合的类型转换")
    public static CopyOnWriteArraySet copyOnWriteArraySet(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Iterable.class.isAssignableFrom(sourceClass)) {
            HashSet temp = copyOnWriteArraySetTempHashSet(targetType);
            CTKit.iterableToCollectionFillIn((Iterable) source, temp, targetType);
            return new CopyOnWriteArraySet(temp);
        }
        if(sourceClass.isArray()) {
            HashSet temp = copyOnWriteArraySetTempHashSet(targetType);
            CTKit.arrayToCollectionFillIn((Object[]) source, temp, targetType);
            return new CopyOnWriteArraySet(temp);
        }
        throw new ClassCastException("无法将 " + sourceClass.getName() +
                " 的 【" + source + "】 转为 java.util.concurrent.CopyOnWriteArraySet");
    }

    @Note("生成其他未知类型的 Set 集合")
    public static Set otherSet(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Iterable.class.isAssignableFrom(sourceClass)) {
            Set list = (Set) ObjKit.buildObject(targetType);
            CTKit.iterableToCollectionFillIn((Iterable) source, list, targetType);
            return list;
        }
        if(sourceClass.isArray()) {
            Set list = (Set) ObjKit.buildObject(targetType);
            CTKit.arrayToCollectionFillIn((Object[]) source, list, targetType);
            return list;
        }
        throw new ClassCastException("无法将 " + sourceClass.getName() +
                " 的 【" + source + "】 转为 " + targetType.getMainType().getName());
    }


    @Note("OwnSet 集合的类型转换")
    public static OwnSet ownSet(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Iterable.class.isAssignableFrom(sourceClass)) {
            OwnSet set = ownSet(targetType);
            CTKit.iterableToOwnRowsFillIn((Iterable) source, set, targetType);
            return set;
        }
        if(sourceClass.isArray()) {
            OwnSet set = ownSet(targetType);
            CTKit.arrayToOwnRowsFillIn((Object[]) source, set, targetType);
            return set;
        }
        throw new ClassCastException("无法将 " + sourceClass.getName() +
                " 的 【" + source + "】 转为 com.tools.common.container.OwnSet");
    }


    @Note("生成其他未知类型的 AbsOwnSet 集合")
    public static AbsOwnSet otherOwnSet(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Iterable.class.isAssignableFrom(sourceClass)) {
            AbsOwnSet set = (AbsOwnSet) ObjKit.buildObject(targetType);
            CTKit.iterableToOwnRowsFillIn((Iterable) source, set, targetType);
            return set;
        }
        if(sourceClass.isArray()) {
            AbsOwnSet set = (AbsOwnSet) ObjKit.buildObject(targetType);
            CTKit.arrayToOwnRowsFillIn((Object[]) source, set, targetType);
            return set;
        }
        throw new ClassCastException("无法将 " + sourceClass.getName() +
                " 的 【" + source + "】 转为 com.tools.common.container.AbsOwnSet");
    }

    /* ************************************************************************************************************
     *
     *          私有逻辑
     *
     * ************************************************************************************************************
     * */

    @Note("抽取公共部分，判断参数是否是数组，不是的话那就构建空 HashSet")
    private static HashSet private_newHashSetByOrArray(Object first) {
        if(first != null && first.getClass().isArray()) {
            Object[] f = (Object[]) first;
            HashSet temp = new HashSet(MapEnum.calcInitSize(f.length));
            Collections.addAll(temp, f);
            return temp;
        }
        return new HashSet();
    }



}
